﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;


namespace Model
{
    public class BaseModel : IDisposable
    {        
        #region Fields
        private string _tableName;
        private string _fieldId;
        #endregion

        #region Properties
        public string TableName
        {
            get
            {
                return _tableName;
            }
        }
        #endregion

        #region Constructors
        public BaseModel()
        {
        }
        public BaseModel(string tableName)
        {
            this._tableName = tableName.Trim();
        }
        public BaseModel(string tableName, string fieldId)
            : this(tableName)
        {
            this._fieldId = fieldId.Trim();
        }
        #endregion

        #region Public Methods
        #region DataView
        public static object GetFieldValue(DataRowView row, string FieldName)
        {
            return ( row.Row.Table.Columns.Contains(FieldName) ? row[FieldName] : null );
        }
        public static string GetStringFieldValue ( DataRowView row, string FieldName )
        {
            return ( row.Row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToString(row[FieldName]) : "" );
        }
        public static int GetIntFieldValue ( DataRowView row, string FieldName )
        {
            return ( row.Row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToInt32(row[FieldName]) : 0 );
        }
        public static float GetFloatFieldValue(DataRowView row, string FieldName)
        {
            return ( row.Row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToSingle(row[FieldName]) : 0 );
        }
        public static bool GetBooleanFieldValue ( DataRowView row, string FieldName )
        {
            return ( row.Row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToBoolean(row[FieldName]) : false );
        }
        public static DateTime GetDateTimeFieldValue ( DataRowView row, string FieldName )
        {
            return ( row.Row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToDateTime(row[FieldName]) : DateTime.MinValue );
        }
        #endregion

        #region Static Methods
        public static object GetFieldValue ( DataRow row, string FieldName )
        {
            return ( row.Table.Columns.Contains(FieldName) ? row[FieldName] : null );
        }
        public static string GetStringFieldValue ( DataRow row, string FieldName )
        {
            return ( row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToString(row[FieldName]) : "" );
        }
        public static int GetIntFieldValue ( DataRow row, string FieldName )
        {
            return ( row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToInt32(row[FieldName]) : 0 );
        }
        public static bool GetBooleanFieldValue ( DataRow row, string FieldName )
        {
            return ( row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToBoolean(row[FieldName]) : false );
        }
        public static DateTime GetDateTimeFieldValue ( DataRow row, string FieldName )
        {
            return ( row.Table.Columns.Contains(FieldName) && row[FieldName] != DBNull.Value ? Convert.ToDateTime(row[FieldName]) : DateTime.MinValue );
        }

        public static DataSet GetDataSetBySQL ( string StringSQL )
        {
            SqlDataAdapter da = new SqlDataAdapter(StringSQL, DBConnection.Connection);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
            }
            catch
            {  }

            if (da != null)
                da.Dispose();

            return ds;
        }
        public static DataTable GetAllByTableName ( string TableName )
        {
            string sql = "SELECT * FROM " + TableName;
            return GetDataTableBySQL(sql);
        }
        public static DataTable GetDataTableBySQL ( string stringSQL )
        {
            DataSet ds = GetDataSetBySQL(stringSQL);
            DataTable dt = new DataTable();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                ds.Dispose();
            }
            return dt;
        }
        #endregion

        public virtual DataTable GetAll ()
        {
            return GetAllByTableName(_tableName);       
        }

        public virtual DataTable GetAllByTableName()
        {
            return GetAllByTableName(_tableName); 
        }

        public virtual DataSet GetDataByStoreProcedure(string ProcedureName, params object[] Parameters)
        {
            return GetDataSet(ProcedureName, Parameters);
        }

        public static DataTable GetDataTable(int TableIndex, string ProcedureName, params object[] Parameters)
        {
            DataSet ds = GetDataSet(ProcedureName, Parameters);
            DataTable dt = null;
            if (ds != null && ds.Tables.Count > 0)
            {
                if (TableIndex >= 0 && TableIndex < ds.Tables.Count)
                    dt = ds.Tables[TableIndex];

                ds.Dispose();
            }
            return dt;
        }

        public static DataSet GetDataSet(string ProcedureName, params object[] Parameters)
        {
            return SqlHelper.ExecuteDataset(DBConnection.Connection, ProcedureName, Parameters);
            
        }

        public static DataTable GetDataTable(string ProcedureName, params object[] Parameters)
        {
            return GetDataTable(0, ProcedureName, Parameters);
        }

        public virtual bool IsExist(string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(ProcedureName, Parameters);
            bool bIsExist = (dt != null && dt.Rows.Count > 0);

            if (dt != null)
                dt.Dispose();

            return bIsExist;
        }

        public virtual bool IsExist(string stringSQL)
        {
            DataTable dt = GetDataTableBySQL(stringSQL);
            bool bIsExist = (dt != null && dt.Rows.Count > 0);

            if (dt != null)
                dt.Dispose();

            return bIsExist;
        }

        public virtual bool IsExistByTableName(string tableName)
        {
            DataTable dt = GetDataTableBySQL("select top 1 1 from " + tableName);
            bool bIsExist = (dt != null && dt.Rows.Count > 0);

            if (dt != null)
                dt.Dispose();

            return bIsExist;
        }

        public virtual DataRow GetDataRow(string ProcedureName, params object[] Parameters)
        {
            return GetDataRow(0, ProcedureName, Parameters);
        }

        public virtual DataRow GetDataRowBySQL(string stringSQL)
        {
            DataTable dt = GetDataTableBySQL(stringSQL);
            DataRow dr = null;

            if (dt != null && dt.Rows.Count > 0)
            {
                dr = dt.Rows[0];
                dt.Dispose();
            }
            return dr;
        }

        public virtual DataRow GetDataRow(int RowIndex, string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(0, ProcedureName, Parameters);
            DataRow dr = null;

            if (dt != null)
            {
                if (RowIndex >= 0 && RowIndex < dt.Rows.Count)
                {
                    dr = dt.Rows[RowIndex];
                }
                dt.Dispose();
            }
            return dr;
        }

        public virtual DataColumn GetDataColumn(int ColumnIndex, string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(0, ProcedureName, Parameters);
            DataColumn dc = null;

            if (dt != null)
            {
                if (ColumnIndex >= 0 && ColumnIndex < dt.Columns.Count)
                {
                    dc = dt.Columns[ColumnIndex];
                }
                dt.Dispose();
            }
            return dc;
        }

        public virtual DataColumn GetDataColumn(string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(0, ProcedureName, Parameters);
            DataColumn dc = null;

            if (dt != null && dt.Columns.Count > 0)
            {
                dc = dt.Columns[0];
                dt.Dispose();
            }
            return dc;
        }

        public virtual DataColumn GetDataColumn(string ColumnName, string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(0, ProcedureName, Parameters);
            DataColumn dc = null;

            if (dt != null)
            {
                if (dt.Columns.Contains(ColumnName))
                {
                    dc = dt.Columns[ColumnName];
                }
                dt.Dispose();
            }
            return dc;
        }

        public virtual DataSet GetDataByFunction(string FunctionName, params object[] Parameters)
        {
            return GetDataSet(FunctionName, Parameters);
        }

        public virtual DataSet GetDataByView(string ViewName)
        {
            return SqlHelper.ExecuteDataset(DBConnection.Connection, ViewName);
        }

        public virtual object ExecuteScalar(string ProcedureName, params object[] Parameters)
        {
            return SqlHelper.ExecuteScalar(DBConnection.Connection, ProcedureName, Parameters);
        }

        public virtual object GetValueFromDB(string ProcedureName, params object[] Parameters)
        {
            return GetValueFromDB(0, 0, ProcedureName, Parameters);
        }

        public virtual object GetValueFromDBBySQL(string stringSQL)
        {
            DataTable dt = GetDataTableBySQL(stringSQL);
            object Obj = null;

            if (dt != null && dt.Rows.Count > 0)
            {
                Obj = dt.Rows[0][0];
                dt.Dispose();
            }
            return Obj;
        }

        public virtual object GetValueFromDBBySQL(string stringSQL, int ColumnIndex)
        {
            DataTable dt = GetDataTableBySQL(stringSQL);
            object Obj = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                if (ColumnIndex >= 0 && ColumnIndex < dt.Columns.Count)
                {
                    Obj = dt.Rows[0][ColumnIndex];
                }
                dt.Dispose();
            }
            return Obj;
        }

        public virtual object GetValueFromDBBySQL(string stringSQL, string ColumnName)
        {
            DataTable dt = GetDataTableBySQL(stringSQL);
            object Obj = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Columns.Contains(ColumnName))
                {
                    Obj = dt.Rows[0][ColumnName];
                }
                dt.Dispose();
            }
            return Obj;
        }

        public virtual object GetValueFromDB(int ColumnIndex, string ProcedureName, params object[] Parameters)
        {
            DataTable dt  = GetDataTable(ProcedureName, Parameters);
            object Obj = null;

            if (dt != null && dt.Rows.Count > 0)
            {
                if (ColumnIndex >= 0 && ColumnIndex < dt.Columns.Count)
                {
                    Obj = dt.Rows[0][ColumnIndex];
                }
                dt.Dispose();
            }
            return Obj;
        }

        public virtual object GetValueFromDB(int RowIndex, int ColumnIndex, string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(ProcedureName, Parameters);
            object Obj = null;

            if (dt != null && dt.Rows.Count > 0)
            {
                if (ColumnIndex >= 0 && ColumnIndex < dt.Columns.Count && RowIndex >= 0 && RowIndex < dt.Rows.Count)
                {
                    Obj = dt.Rows[RowIndex][ColumnIndex];
                }
                dt.Dispose();
            }
            return Obj;
        }

        public virtual object GetValueFromDB(string ColumnName, string ProcedureName, params object[] Parameters)
        {
            DataTable dt = GetDataTable(ProcedureName, Parameters);
            object Obj = null;

            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Columns.Contains(ColumnName))
                {
                    Obj = dt.Rows[0][ColumnName];
                }
                dt.Dispose();
            }
            return Obj;
        }

        public virtual int ExecuteNonQuery(string ProcedureName, params object[] Parameters)
        {
            return SqlHelper.ExecuteNonQuery(DBConnection.Connection, ProcedureName, Parameters);
        }

        public virtual void ExecuteSQL(string stringSQL)
        {
            SqlCommand cmd = new SqlCommand(stringSQL, DBConnection.Connection);
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch
            {   }
            cmd.Dispose();
        }

        public virtual void ExecuteNonDataQuery(string ProcedureName, params object[] Parameters)
        {
            SqlHelper.ExecuteNonQuery(DBConnection.Connection, ProcedureName, Parameters);
        }

        public virtual DateTime GetCurentDate()
        {
            return Convert.ToDateTime(GetValueFromDB("GetCurentDate"));
        }

        #endregion
        
        #region IDisposable Members

        public void Dispose ()
        {
            //
        }

        #endregion
    }
}
